/**
 * 创建公开型项目第二步
 */
function SkcSelectCourse() {
    var _this = this;
    var projectId = $("#id").val();                 // 项目编号

    _this.init = function () {
        _this.initEvent();
    }

    _this.initEvent = function() {
        // 数据处理 （清除project_course_info表数据）
        $("#btn_handle_data").click(function () {
            var html = "<div class='box' id='addLayer'>";
            html += "<form  id='skcForm' class='search-group'  type='POST'>";
            html += "<input type='hidden' id='project_id' name='project_id' value='" + projectId + "'/>";
            html += "<label for='' class='col-3' style='width:50px;margin-left: 20px;'>秘钥：</label>";
            html += "<div class='col-9'><input type='text' style='width:480px;' class='textarea' id='token' name='token'  placeholder='请输入秘钥'></div>";
            html += "<a href='javascript:;'  class='btn btn-info sub' style='margin-top: 30px;' onclick='handleDate(1)'>提交</a>";
            html += "</form>";
            html += "</div>";
            _this.index = layer.open({
                type: 1,
                title: '数据处理',
                shadeClose: true, // true点击遮罩层关闭
                area: ['600px', '250px'], // 弹出层大小
                scrollbar: false, // false隐藏滑动块
                content: html
            });

        });
        //skc定制课程弹窗
        $("#btn_skc_course").click(function () {
            $.ajax({
                url: appPath + '/project/skc/selectOne',
                dataType: 'json',
                async: false,
                type: 'post',
                data: {
                    'project_id': projectId
                },
                success: function (data) {
                    var result = data.code;
                    var projectSkcCourse = data.result;
                    if (10000 == result) {
                        _this.editSkcCourse(projectSkcCourse);
                    } else {
                        layer.alert('操作失敗', {icon: 2});
                    }
                }
            });

        });


        // skc课程编辑框
        _this.editSkcCourse = function (projectSkcCourse) {
            var html = "<div class='box' id='addLayer'>";
            html += "<form  id='skcForm' class='search-group'  type='POST'>";
            html += "<input type='hidden' id='project_id' name='project_id' value='" + projectId + "'/>";
            html += "<label for='' class='col-3' style='width:110px;margin-left: 20px;'>课程编号集合：</label>";
            if (null != projectSkcCourse) {
                var coursenos = projectSkcCourse.course_nos;
                html += "<div class='col-9'><textarea style='width:460px;' class='textarea' id='coursenos' name='coursenos' rows='7' cols='5'   placeholder='课程编号，用逗号隔开'>" + coursenos + "</textarea></div>";
            } else {
                html += "<div class='col-9'><textarea style='width:460px;' class='textarea' id='coursenos' name='coursenos' rows='7' cols='5'   placeholder='课程编号，用逗号隔开'></textarea></div>";
            }
            html += "<a href='javascript:;'  class='btn btn-info sub' onclick='saveSkcCourses(1)'>提交</a>";
            html += "</form>";
            html += "</div>";
            _this.index = layer.open({
                type: 1,
                title: '课程编号集合',
                shadeClose: true, // true点击遮罩层关闭
                area: ['600px', '250px'], // 弹出层大小
                scrollbar: false, // false隐藏滑动块
                content: html
            });
        }

    }
}
// 输入秘钥处理数据
function handleDate(index) {
    var token = $("#token").val();
    if("" == token){
        layer.msg("请输入秘钥");
        return;
    };
    token = $.trim(token);

    $.ajax({
        url: appPath + '/project/skc/project_course_info/clear',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {
            'token': token
        },
        success: function (data) {
            var code = data.code;
            var msg = data.result;
            if (10000 == code) {
                layer.msg("数据处理成功");
                layer.close(index);
            } else {
                layer.alert(msg, {icon: 2});
            }
        }
    });

}

// 提交定制课程保存数据到mongo
function saveSkcCourses(index) {
    var coursenos = $("#coursenos").val();
    if("" == coursenos){
        layer.msg("请输入课程编号");
        return;
    };
    // 处理中文逗号
    coursenos = coursenos.replace(/，/ig,',');
    $("#coursenos").val($.trim(coursenos));

    var url = appPath +  "/project/skc/updateSkcCourseNos";
    var opt = {
        url: url,
        type: "post",
        dataType: "json",
        success: function (data) {
            var  result = data.code;
            if ("10000" == result) {
                layer.msg("保存成功");
                layer.close(index);
            }
        }
    }
    $("#skcForm").ajaxSubmit(opt);

}

var skc_select_course = new SkcSelectCourse();